package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsAdminServiceImpl implements DtsAdminService {
    @Autowired
    DtsAdminMapper dtsAdminMapper;
    @Override
    public DtsAdmin findUsername(String username) {
        LambdaQueryWrapper<DtsAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DtsAdmin::getUsername,username);
        DtsAdmin dtsAdmin = dtsAdminMapper.selectOne(queryWrapper);
        return dtsAdmin;
    }

    @Override
    public IPage<DtsAdmin> findByPage(Integer page, Integer limit, String sort, String order, String username) {
        IPage<DtsAdmin> iPage = new Page<>(page,limit);
        QueryWrapper<DtsAdmin> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            qw.like("username",username);
        }
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }

        dtsAdminMapper.selectPage(iPage, qw);
        return iPage;
    }

    @Override
    public DtsAdmin add(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(encode);


        dtsAdminMapper.insert(admin);
        return admin;
    }

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        String password = admin.getPassword();
        String passwordDB = dtsAdminMapper.selectById(admin).getPassword();
        if(!passwordDB.equalsIgnoreCase(password)){
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encode = passwordEncoder.encode(admin.getPassword());
            admin.setPassword(encode);
        }
        admin.setUpdateTime(new Date());


        dtsAdminMapper.updateById(admin);
        return admin;
    }

    @Override
    public DtsAdmin delete(DtsAdmin admin) {
        dtsAdminMapper.deleteById(admin);
        return admin;
    }

    @Override
    public List<Map<String, Object>> findAdmin() {
        List<DtsAdmin> dtsAdmins = dtsAdminMapper.selectList(null);
        List<Map<String, Object>> adminlist = dtsAdmins.stream().map(admin -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", admin.getId());
            map.put("label", admin.getUsername());
            return map;
        }).collect(Collectors.toList());
        return adminlist;
    }
}
